package web;

import java.util.ArrayList;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;

import metier.Imetier;
import model.Account;
import model.Module;
import model.Thematic;

import com.opensymphony.xwork2.ActionSupport;

public class GestionAction extends ActionSupport {
	private String v;
	private Long pk;
	private String login;
	private String pass;
	private String type;
	private String titre;
	private String desc;
	private String opt;
	private List<Account> listeAbonnements = new ArrayList<Account>();
	private Account ac = new Account();
   private Thematic theme=new Thematic();
	private List<Thematic> them = new ArrayList<Thematic>();

	
	public Thematic getTheme() {
		return theme;
	}

	public void setTheme(Thematic theme) {
		this.theme = theme;
	}

	public String getTitre() {
		return titre;
	}

	public void setTitre(String titre) {
		this.titre = titre;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public String getOpt() {
		return opt;
	}

	public void setOpt(String opt) {
		this.opt = opt;
	}

	public List<Thematic> getThem() {
		return them;
	}

	public void setThem(List<Thematic> them) {
		this.them = them;
	}

	@Autowired
	private Imetier metier;

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return super.execute();
	}

	public String nouveauAbonnement() {
		String s = "";

		Account a = metier.findcompte(login);

		if (a.getLogin() != null) {

			s = "error";
			addActionError("login existe");
		} else {

			metier.addcompte(new Account(login, pass, type));
			s = "success";
		}
		listeAbonnements = metier.findcompteAll();
		return s;

	}

	public String updateAbonnement() {

		String s = "";

		Account a = metier.findcompte(login);
		if (a.getLogin() != null) {
			if (a.getLogin() != v) {
				s = "error";
				addActionError("login existe");
			}
			if (a.getLogin().equals(v)) {
				metier.updatecompte(new Account(pk, login, pass, type));

				s = "success";
			}
		} else {
			metier.updatecompte(new Account(pk, login, pass, type));

			s = "success";
		}
		listeAbonnements = metier.findcompteAll();
		login = "";
		pass = "";
		type = "";
		return s;
	}

	public String listeAbonnements() {
		listeAbonnements = metier.findcompteAll();
		return "success";
	}

	public String supprimerAbonnement() {

		metier.deletecompte(pk);
		listeAbonnements = metier.findcompteAll();
		return "success";
	}

	public String abonnementsParType() {
		listeAbonnements = metier.findcompteAll();
		ac = metier.findcompte(pk);
		login = ac.getLogin();
		pass = ac.getPass();
		type = ac.getType();
		v = login;
		return "success";
	}

	public String addTheme() {
		String s = "";

		Thematic a = metier.findtheme(titre);

		if (a.getHoti() != null) {

			s = "error";
			addActionError("Theme existe");
		} else {

			metier.addtheme(new Thematic(titre, desc, opt));
			s = "success";
		}
		them = metier.findthemeAll();

		return s;
	}
	public String listeTheme() {
		them = metier.findthemeAll();
		return "success";
	}

	
	@Override
	protected void finalize() throws Throwable {
		// TODO Auto-generated method stub
		super.finalize();
	}

	public GestionAction() {
		super();
	}

	public Long getPk() {
		return pk;
	}

	public void setPk(Long pk) {
		this.pk = pk;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public List<Account> getListeAbonnements() {
		return listeAbonnements;
	}

	public void setListeAbonnements(List<Account> listeAbonnements) {
		this.listeAbonnements = listeAbonnements;
	}

	public Account getAc() {
		return ac;
	}

	public void setAc(Account ac) {
		this.ac = ac;
	}

	public Imetier getMetier() {
		return metier;
	}

	public void setMetier(Imetier metier) {
		this.metier = metier;
	}

}
